*Merges and Labels
* para checar seu "username" no Stata, digite "di c(username)"
else if "`c(username)'" == "Administrador" { 
	global ROOT "C:\Users\Administrador\Meu Drive\00 Pesquisa\# Gender Gap Turnout"
}


if "`c(username)'" == "vitor" {
    global ROOT "G:\.shortcut-targets-by-id\1Gn0Qh8ehM2EKoEEeJt9JcaQDco2Xt0O2\# Gender Gap Turnout"
}
                 
				 
*Merge Election Results and Turnout			 
use "${ROOT}\Dados e Análise\TSE Data\election_results.dta", clear
merge m:1 cd_municipio ano_eleicao turno using "${ROOT}\Dados e Análise\TSE Data\turnout.dta"

/*
Result                           # of obs.
-----------------------------------------
 not matched                        15,360
     from master                    15,346  (_merge==1)
     from using                         14  (_merge==2)

 matched                            49,765  (_merge==3)
-----------------------------------------

tab ano if _merge == 1

ANO_ELEICAO |      Freq.     Percent        Cum.
------------+-----------------------------------
       2012 |     15,326       99.87       99.87
       2020 |         20        0.13      100.00
------------+-----------------------------------
      Total |     15,346      100.00


Most of unmatched cases are from 2012 (we do not have/need the election results) - 15,326 cases 
There are only 20 cases, across 14 municipalities/years/rounds, for which we do not have the election results.
There are also 14 cases where we have the results, but do not have turnout information
*/
drop if _merge == 1
drop if _merge == 2 & ano_eleicao != 2012
drop _merge

*Merge Election Results, Turnout and Candidates
merge 1:1 sq_candidato ano_eleicao cd_municipio turno using "${ROOT}\Dados e Análise\TSE Data\Candidates\candidates.dta"

/*
Result                           # of obs.
-----------------------------------------
not matched                        16,609
    from master                        49  (_merge==1)
    from using                     16,560  (_merge==2)

matched                            49,521  (_merge==3)
-----------------------------------------

tab ano if _merge==2

ANO_ELEICAO |      Freq.     Percent        Cum.
------------+-----------------------------------
       2012 |     15,327       92.55       92.55
       2016 |        394        2.38       94.93
       2020 |        300        1.81       96.75
       2024 |        539        3.25      100.00
------------+-----------------------------------
      Total |     16,560      100.00

	  
Most of unmatched cases (_merge == 2) are from 2012 (we do not have/need the election results) - 15,327 cases 
The other ones are not available in the using dataset. They probably refer to inconsistencies in the original database, 
but since they are not in the turnout and the election results databases, they will be dropped out. 
The unmatched cases (_merge == 1) exist in the master database, but not in the using one, so they will be dropped out.
*/

drop if _merge == 2 | _merge == 1
drop _merge nm_candidato2 concat codigo_candidato candidatura

merge m:1 cd_municipio using "${ROOT}\Dados e Análise\TSE Data\IBGE.dta"
/*
    Result                           # of obs.
    -----------------------------------------
    not matched                             2
        from master                         2  (_merge==1)
        from using                          0  (_merge==2)

    matched                            49,519  (_merge==3)
    -----------------------------------------

*/
drop if _merge == 1
drop _merge

merge m:1 cd_municipio using "${ROOT}\Dados e Análise\TSE Data\Results\vote_share_bolsonaro22.dta"

/*
    Result                           # of obs.
    -----------------------------------------
    not matched                           142
        from master                         0  (_merge==1)
        from using                        142  (_merge==2)

    matched                            49,519  (_merge==3)
    -----------------------------------------
*/
drop if _merge == 2
drop _merge

label variable ano_eleicao              "Election year"
label variable turno                    "Election round"
label variable uf                       "State abbreviation"
label variable cd_municipio             "Municipality code (TSE)"
label variable sq_candidato             "Candidate ID"
label variable nm_candidato             "Candidate name"
label variable qt_votos_nominais        "Number of nominal votes"
label variable rank_votos               "Candidate's vote rank in municipality"
label variable segundo_turno_mun        "Municipality had a runoff election"
label variable eleito_primeiro_turno    "Elected in first round"
label variable eleito_segundo_turno     "Elected in second round"
label variable eleito                   "Elected (1=yes)"
label variable reeleicao_tentativa      "Incumbent seeking reelection"
label variable reeleicao_sucesso        "Successful reelection"
label variable regiao                   "Geographic region"

label variable aptos_mulheres_100_mais  "Registered female voters (age 100+)"
label variable comparecimento_mulheres_100_mais "Female turnout (age 100+)"
label variable abstencao_mulheres_100_mais "Female abstention (age 100+)"
label variable aptos_homens_100_mais    "Registered male voters (age 100+)"
label variable comparecimento_homens_100_mais "Male turnout (age 100+)"
label variable abstencao_homens_100_mais "Male abstention (age 100+)"

foreach s in 16 17 18 19 20 21_24 25_29 30_34 35_39 40_44 45_49 50_54 55_59 60_64 65_69 70_74 75_79 80_84 85_89 90_94 95_99 {
    label variable aptos_mulheres_`s'           "Registered female voters (age `s')"
    label variable comparecimento_mulheres_`s'  "Female turnout (age `s')"
    label variable abstencao_mulheres_`s'       "Female abstention (age `s')"
    label variable aptos_homens_`s'             "Registered male voters (age `s')"
    label variable comparecimento_homens_`s'    "Male turnout (age `s')"
    label variable abstencao_homens_`s'         "Male abstention (age `s')"
}

label variable abstencao_total_homens        "Total male abstentions (all ages)"
label variable abstencao_total_mulheres      "Total female abstentions (all ages)"
label variable aptos_total_homens            "Total eligible male voters (all ages)"
label variable aptos_total_mulheres          "Total eligible female voters (all ages)"
label variable comparecimento_total_homens   "Total male turnout (all ages)"
label variable comparecimento_total_mulheres "Total female turnout (all ages)"
label variable abstencao_total               "Total abstentions (men and women, all ages)"
label variable aptos_total                   "Total eligible voters (men and women, all ages)"
label variable comparecimento_total          "Total turnout (men and women, all ages)"


label variable partido                 "Candidate's political party"
label variable idade                   "Candidate age (years)"
label variable genero                  "Candidate gender"
label variable escolaridade            "Candidate education level"
label variable estado_civil            "Candidate marital status"
label variable raca                    "Candidate race"
label variable ocupacao                "Candidate occupation"

label variable codigo_mun_ibge         "Municipality code (IBGE, Census 2022)"
label variable rendimento_medio_pop    "Average income per capita (Census 2022)"
label variable populacao_mun           "Total population (Census 2022)"
label variable densidade_pop_mun       "Population density (Census 2022)"
label variable p_pop_mulheres          "Percentage of women in population (Census 2022)"
label variable p_pop_urbana            "Percentage of urban population (Census 2022)"
label variable p_alfabetizados         "Literacy rate (Census 2022)"
label variable p_estudos_8anos_mais    "Share with 8+ years of schooling (Census 2022)"
label variable p_negros                "Percentage of Black and Brown population (Census 2022)"
label variable population_log          "Log of total population (Census 2022)"
label variable rendimento_medio_log    "Log of average income (Census 2022)"
label variable densidade_pop_log       "Log of population density (Census 2022)"
label variable vote_share_bolsonaro    "Bolsonaro vote share in municipality (2022 second round)"





save "${ROOT}\Dados e Análise\TSE Data\base_tratada.dta", replace



